package com.ccx.service.impl;

import com.ccx.mapper.EbUserMapper;
import com.ccx.model.EbUser;
import com.ccx.model.EbUserExample;
import com.ccx.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServicelmpl implements UserService {
    @Autowired
    private EbUserMapper ebUserMapper;


    @Override
    public List<EbUser> list(int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<EbUser> ebUsers = ebUserMapper.selectByExample(null);

        return ebUsers;
    }

    @Override
    public Integer save(EbUser ebUser) {
        return ebUserMapper.insert(ebUser);
    }

    @Override
    public EbUser selectOne(String euUserId) {
        return  ebUserMapper.selectOne(euUserId);
    }

    @Override
    public Integer updateUser(EbUser ebUser) {
        return ebUserMapper.updateByPrimaryKey(ebUser);
    }

    @Override
    public int delete(EbUser ebUser){
        return  ebUserMapper.deleteByPrimaryKey(ebUser.getEuUserId());

    }




    //前台用户登录功能
    @Override
    public EbUser login(String userName, String passWord) {

        EbUserExample example = new EbUserExample();
        //拼接查询条件
        EbUserExample.Criteria criteria = example.createCriteria();
        criteria.andEuUserNameEqualTo(userName);
        criteria.andEuPasswordEqualTo(passWord);//相当于在数据库中select * from user where username=? and password=?
        List<EbUser>list = ebUserMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }


}
